/*
 *	Copyright © 2008 University of Houston
 *	All rights reserved.
 */

#include <ctype.h>
#include <iostream>
#include <pcap.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include "analyzer.h"
#include "listener.h"
#include "logger.h"
#include "session.h"
#include "util.h"

#define DEFAULT_IFACE	""
#define DEFAULT_PORT	"22"
#define DEFAULT_HOST	""
#define DEFAULT_FILTER	"0"
#define DEFAULT_ALG		"0"

using namespace std;

void displayUsage(char **argv);
bool getParams(int argc, char** argv, string *iface, string *port,
			   string *host, string *logfile, string *filter, string *alg);

void *listenerThread(void* args)
{
	Listener *myListener = (Listener*)args;

	myListener->run();

	return NULL;
}

int main(int argc, char **argv)
{
	// Display licensing agreement.
	cout << "*	Copyright © 2008 University of Houston\n"
	"*	All rights reserved." << endl;

	// Make sure we are running as root.
	if ( getuid() != 0 )
	{
		FATAL("** You must be running as root **")
		return 1;
	}

	// Get our command-line parameters
	string interface 	= DEFAULT_IFACE;
	string port 		= DEFAULT_PORT;
	string host			= DEFAULT_HOST;
	string fil			= DEFAULT_FILTER;
	string alg			= DEFAULT_ALG;
	string logfile		= DEFAULT_LOGFILE;
	if ( !getParams(argc, argv, &interface, &port, &host, &logfile, &fil, &alg) )
		return -1;

	// Print selected information
	INFO("Device: " 		<< interface)
	INFO("Port filter: " << port)
	INFO("Filter: "		<< fil)
	INFO("Algorithm: "	<< alg)
	INFO("Log file: " 	<< logfile)

	// Create the listener object
	Listener listener(interface, port, host, logfile, strtol(fil.c_str()), strtol(alg.c_str()));

	// Create the thread that does the actual listening
	pthread_t pListenerThread;
	pthread_create(&pListenerThread, NULL, listenerThread, &listener);

	// Wait for the user to tell us to stop...
	INFO("### Press enter to end packet capture ###")
	string temp;
	getline(cin,temp);
	INFO(   "\n\t================================\n"
		 	  "\t        Capture complete        \n"
		 	  "\t================================\n")

	// Kill the thread
	pthread_cancel(pListenerThread);

	return 1;
}

/*
 * Checks the command-line arguments, and parses them into distinct commands.
 * @param argc 		[argc] as provided on the command-line.
 * @param argv 		[argv] as provided on the command-line.
 * @param iface 	Pointer to a string to store the interface name.
 * @param port 		Pointer to a string to store the port number.
 * @param logfile 	Pointer to a string to store the log file's path.
 * @param filter 	Pointer to a string to store the filter name.
 * @param alg		Pointer to a string to store the algorithm's name.
 * @return 			False if there were not enough parameters provided.
 */
bool getParams(int argc, char** argv, string *iface, string *port,
			   string *host, string *logfile, string *filter, string *alg)
{
	int index;
	int c;
	opterr = 0;

	while ((c = getopt (argc, argv, "i:p:t:l:f:a:h?")) != -1)
		switch (c)
		{
		case 'i':
			*iface 	= optarg;
			break;
		case 'p':
			*port 	= optarg;
			break;
		case 't':
			*host	= optarg;
			break;
		case 'l':
			*logfile = optarg;
			break;
		case 'f':
			*filter = optarg;
			break;
		case 'a':
			*alg = optarg;
			break;
		case 'h':
		case '?':
			displayUsage(argv);
			return false;
		default:
			ERROR("Option \'-" << optopt << "\' not supported.")
			break;
		}

	for (index = optind; index < argc; index++)
		ERROR("Non-option argument " << argv[index]);

	return true;
}

/*≈Ωç√∫åß∂ƒ©œ∑
 * Display the usage
 */
void displayUsage(char **argv)
{
	printf("Usage: %s [args]\n"
		   "Supported Arguments: \n"
		   "-a n         Heuristic algorithm number alg. Default %s.\n"
		   "-f n         Packet filter.  Default %s.\n"
		   "-i iface     Interface to listen on.  Default selected by pcap.\n"
		   "-l path      Path the the logfile.  Default \'%s\'\n"
		   "-p n         Port to listen on.  Default %s.\n"
		   "-t ipAddr    Accept packets only from specified host(s).\n",
		argv[0], DEFAULT_ALG, DEFAULT_FILTER, DEFAULT_LOGFILE, DEFAULT_PORT);
}


